﻿namespace Microsoft.SharePoint.Client
{
    using System;
    using System.ComponentModel;

    [ScriptType("SP.WebCollection", ServerTypeId="{c197d59e-d070-43bf-ad5e-10d6152e38a6}")]
    public class WebCollection : ClientObjectCollection<Web>
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public WebCollection(ClientRuntimeContext Context, ObjectPath ObjectPath) : base(Context, ObjectPath)
        {
        }

        [Remote]
        public Web Add(WebCreationInformation parameters)
        {
            if (base.Context.ValidateOnClient)
            {
                if (parameters == null)
                {
                    throw ClientUtility.CreateArgumentNullException("parameters");
                }
                if (parameters != null)
                {
                    if (parameters.Url == null)
                    {
                        throw ClientUtility.CreateArgumentNullException("parameters.Url");
                    }
                    if ((parameters.Url != null) && (parameters.Url.Length > 0x80))
                    {
                        throw ClientUtility.CreateArgumentException("parameters.Url");
                    }
                    if ((parameters.Url.StartsWith("/", StringComparison.OrdinalIgnoreCase) || parameters.Url.StartsWith(".", StringComparison.OrdinalIgnoreCase)) || (parameters.Url.EndsWith("/", StringComparison.OrdinalIgnoreCase) || parameters.Url.EndsWith(".", StringComparison.OrdinalIgnoreCase)))
                    {
                        throw ClientUtility.CreateArgumentException("parameters.Url");
                    }
                    if (parameters.Url.IndexOf("/wpresources", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        throw ClientUtility.CreateArgumentException("parameters.Url");
                    }
                    if ((parameters.Url.IndexOf("//", StringComparison.OrdinalIgnoreCase) >= 0) || (parameters.Url.IndexOf("..", StringComparison.OrdinalIgnoreCase) >= 0))
                    {
                        throw ClientUtility.CreateArgumentException("parameters.Url");
                    }
                    if ((parameters.Title != null) && (parameters.Title.Length > 0xff))
                    {
                        throw ClientUtility.CreateArgumentException("parameters.Title");
                    }
                }
            }
            Web web = new Web(base.Context, new ObjectPathMethod(base.Context, base.Path, "Add", new object[] { parameters }));
            web.Path.SetPendingReplace();
            ObjectIdentityQuery query = new ObjectIdentityQuery(web.Path);
            base.Context.AddQueryIdAndResultObject(query.Id, web);
            base.Context.AddQuery(query);
            base.AddChild(web);
            web.InitFromCreationInformation(parameters);
            return web;
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            bool flag = base.InitOnePropertyFromJson(peekedName, reader);
            if (flag)
            {
                return flag;
            }
            return flag;
        }
    }
}

